<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>你最喜欢哪个明星？</title>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <style>
        /* 图片容器确保统一大小 */
        .star-container {
            width: 200px;
            height: 200px;
            display: inline-block;
            margin: 10px;
            overflow: hidden;
        }
        
        /* 图片本身强制统一大小 */
        .star-container img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            display: block; /* 移除图片底部可能的空白 */
        }
    </style>
</head>
<body>
    <div id="app">
        <h1>你最喜欢哪个明星？</h1>
        
        <!-- 全选复选框 -->
        <div>
            <input type="checkbox" id="selectAllCheckbox" v-model="allChecked" @change="handleAllCheck">
            <label for="selectAllCheckbox">全选</label>
        </div>
        
        <div id="checkboxContainer">
            <span v-for="(item, index) in list" :key="item.id">
                <input type="checkbox" 
                       :id="item.id" 
                       @change="checkClick()" 
                       v-model="item.checked">
                <label :for="item.id">{{ item.name }}</label>
            </span>
        </div>
        
        <div>
            <label for="other">新增其他：</label>
            <input type="text" id="other" v-model="otherStar">
        </div>
        
        <div>
            <input type="file" id="file" @change="handleFileChange" accept="image/*">
            <button @click="uploadCustomStar">上传</button>
        </div>
        
        <div>
            <button @click="selectAll">全选</button>
            <button @click="deselectAll">全不选</button>
            <button @click="invertSelection">反选</button>
        </div>
        
        <h2>推荐明星</h2>
        
        <div>
            <!-- 使用统一的容器包裹图片 -->
            <span v-for="(item, index) in imgs" :key="index + 'img'">
                <div class="star-container">
                    <img :src="item.src" :alt="item.name">
                </div>
            </span>
        </div>
    </div>

    <script>
        const { createApp } = Vue;
        
        createApp({
            data() {
                return {
                    list: [
                        { id: 'star1', name: '赵丽颖', checked: false, src: '/1.png' },
                        { id: 'star2', name: '杨幂', checked: false, src: '/2.png' },
                        { id: 'star3', name: '田栩宁', checked: false, src: '/3.png' },
                        { id: 'star4', name: '梓渝', checked: false, src: '/4.png' }
                    ],
                    otherStar: '',
                    selectedFile: null,
                    imgs: [],
                    allChecked: false
                }
            },
            methods: {
                selectAll() {
                    this.list.forEach(item => {
                        item.checked = true;
                    });
                    this.allChecked = true;
                    this.checkClick();
                },

                deselectAll() {
                    this.list.forEach(item => {
                        item.checked = false;
                    });
                    this.allChecked = false;
                    this.checkClick();
                },

                invertSelection() {
                    this.list.forEach(item => {
                        item.checked = !item.checked;
                    });
                    this.updateAllCheckedStatus();
                    this.checkClick();
                },

                checkClick() {
                    this.imgs = this.list.filter(item => item.checked);
                    this.updateAllCheckedStatus();
                },

                handleAllCheck() {
                    this.list.forEach(item => {
                        item.checked = this.allChecked;
                    });
                    this.checkClick();
                },

                updateAllCheckedStatus() {
                    const checkedCount = this.list.filter(item => item.checked).length;
                    this.allChecked = checkedCount > 0 && checkedCount === this.list.length;
                },

                handleFileChange(e) {
                    this.selectedFile = e.target.files[0];
                },

                uploadCustomStar() {
                    if (!this.otherStar.trim() || !this.selectedFile) {
                        alert('请输入明星名称并选择图片');
                        return;
                    }
                    
                    const newId = 'star' + (this.list.length + 1);
                    const imageUrl = URL.createObjectURL(this.selectedFile);
                    
                    this.list.push({
                        id: newId,
                        name: this.otherStar,
                        checked: true,
                        src: imageUrl
                    });
                    
                    this.otherStar = '';
                    this.selectedFile = null;
                    document.getElementById('file').value = '';
                    this.checkClick();
                }
            },
            mounted() {
                this.checkClick();
            }
        }).mount('#app');
    </script>
</body>
</html>
